package com.lsy.onehot.rnn.qa;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.Map;

import com.lsy.lstm.ai.rnn.qa.dataset.io.CorpusHandle;
import com.lsy.onehot.rnn.qa.inner.ICurpusMateSet;
import com.lsy.onehot.rnn.qa.inner.IOneHotProcess;

public class OneHotProcess implements IOneHotProcess{

	private ICurpusMateSet mate;
	private String out_q = "chat/vec/one-hot-q.vec"  ;
	private String out_a = "chat/vec/one-hot-a.vec"  ;

	public OneHotProcess() {
		// TODO Auto-generated constructor stub
	}
	public File getOutVec(){
		return CorpusHandle.getFileToTempPath(out_q) ;
	}
	
	public void setICurpusMateSet(ICurpusMateSet mate){
		this.mate = mate ;
	}
	public ICurpusMateSet getICurpusMateSet(){
		return mate;
	}
	
	public void init()throws Exception {
//		outputAnswerVecQ();
		outputAnswerVecA();
	}
	
	public void outputAnswerVecQ() throws Exception{
		System.out.println("convert Qestion file to verctor..");
		outputAnswerVec( CorpusHandle.getFileToTempPath(out_q), mate.getDict_Q() ) ;
		System.out.println("convert Qestion file to verctor done..");

	}
	
	public void outputAnswerVecA() throws Exception{
		System.out.println("convert answer file to verctor..");
		outputAnswerVec(CorpusHandle.getFileToTempPath(out_a), mate.getDict_A() ) ;
		System.out.println("convert answer file to verctor done..");
	}
	
	public void outputAnswerVec(File out,Map<Long, String> map) throws Exception {
	    LinkedList<Character> list = mate.getListChar() ;
	    System.out.println("convert  file to verctor .." + map.size()+ " list:"+ list.size() );
	    
	    FileWriter fWriter = new FileWriter(out);
	    BufferedWriter bfw = new BufferedWriter(fWriter);
	    String str; 
		for (Long key : map.keySet()) { 
			if(key%1000 ==0)
			{
				long l1 = key;
				long l2 = map.size();
				System.out.println("key:"+ key + " total:"+ map.size() + " complet:("+( l1 /l2 )*100 + "%)");
			}
		    int[] vector = new int[list.size()];
		    char[] s = map.get(key).toCharArray();
		    for (char c : s) {
		        int index = list.indexOf(c);
		        if (index != -1) vector[index] = 1;
			}
		    str = key + "";
		    for (int n : vector) {
		       str += "," + n;
		    }
		    bfw.write(str + "\n");
		} 
	    bfw.flush();
	    bfw.close();
	}
	
}
